﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.FrameWork;
using LTJ.Common;
using LTJ.Entity;
using LTJ.Web.UI;
using System.Text.RegularExpressions;

namespace uswinweb.datacontrol
{
    /// <summary>
    /// 权限控制列表
    /// </summary>
    public partial class authorityList : BaseWebControl
    {
        protected DataTable maindt = new DataTable();
        protected DataTable subdt = new DataTable();
        protected CompanyInfo cinfo = new CompanyInfo();
        protected Authority ainfo = new Authority();
        protected int cid = SASRequest.GetInt("cid", 0);
        protected int uid = SASRequest.GetInt("uid", 0);

        public override void ShowUserControl()
        {
            maindt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.MainMenu);
            subdt = Caches.GetUserMenu(LTJ.Entity.UserMenuType.SubMenu);

            if (cid > 0 && uid > 0)
            {
                ainfo = Users.GetAuthInfo(cid, uid);
            }
            else if (cid > 0)
            {
                cinfo = Companys.GetCompanyByID(cid);
                ainfo = Users.GetAuthInfo(cid, cinfo.Manager);
            }
        }

        /// <summary>
        /// 是否选中验证
        /// </summary>
        /// <param name="vaildType">验证类型（1，链接；2，动作）</param>
        /// <param name="vaildvalue">验证值</param>
        /// <param name="pagelink">链接</param>
        public string GetChecked(int vaildType, string vaildvalue, DataRow drc)
        {
            string rstr = "";

            if (ainfo == null || ainfo.Authoritys.Trim(',').Length == 0) return "";            

            if (vaildType == 1)
            {
                if (("," + ainfo.Authoritys).Contains("," + vaildvalue + "|")) rstr = "checked='checked'";
            }
            else if (vaildType == 2)
            {
                string authstr = "";
                Regex objStr = new Regex("," + drc["menulink"] + @"\|([a-zA-Z]+?\|)+" + drc["frameid"] + ",", RegexOptions.IgnoreCase);
                Match m = objStr.Match("," + ainfo.Authoritys);
                if (m.Success) authstr = m.Groups[0].Value;
                if (authstr != "" && authstr.Contains("|" + vaildvalue + "|")) rstr = "checked='checked'";
                //rstr = authstr;
            }

            return rstr;
        }
    }
}